package com.ems.service.impl;

import com.ems.entity.BatteryBoxEntity;
import com.ems.mapper.BatteryBoxMapper;
import com.ems.service.IAlarmService;
import com.ems.service.IBatteryBoxService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ems.service.IBatteryTempService;
import com.ems.service.IBatteryVoltageService;
import com.ems.util.SnowflakeIdWorker;
import com.ems.util.StateEnum;
import com.ems.util.StateException;
import com.ems.vo.AlarmVo;
import com.ems.vo.BatteryBoxVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 电池簇 服务实现类
 * </p>
 *
 * @author WuZH
 * @since 2024-04-23
 */
@Service
public class BatteryBoxServiceImpl extends ServiceImpl<BatteryBoxMapper, BatteryBoxEntity> implements IBatteryBoxService {

    @Resource
    private IAlarmService iAlarmService;
    @Resource
    private BatteryBoxMapper batteryBoxMapper;
    @Resource
    private IBatteryVoltageService iBatteryVoltageService;
    @Resource
    private IBatteryTempService iBatteryTempService;

    @Transactional ( rollbackFor = Exception.class, propagation = Propagation.REQUIRED )
    @Override
    public BatteryBoxVo saveSingle ( BatteryBoxVo batteryBoxVo ) {
        if ( batteryBoxVo == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        BatteryBoxEntity batteryBoxEntity = new BatteryBoxEntity ();
        BeanUtils.copyProperties ( batteryBoxVo, batteryBoxEntity );
        batteryBoxEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        batteryBoxEntity.setCreateTime ( new Date () );
        if ( batteryBoxVo.check () ) {
            int insert = baseMapper.insert ( batteryBoxEntity );
            if ( insert == 0 ) {
                throw new StateException ( StateEnum.INSERT_EXCE );
            }
        }
        //BMS警告
        if ( batteryBoxVo.getAlarmVoList () != null && batteryBoxVo.getAlarmVoList ().size () > 0 ) {
            for ( AlarmVo alarmVo : batteryBoxVo.getAlarmVoList () ) {
                iAlarmService.saveSingle ( alarmVo );
            }
        }
        //BMS温度
        if ( batteryBoxVo.getBatteryTempVo () != null ) {
            batteryBoxVo.getBatteryTempVo ().setDeviceAddress ( batteryBoxVo.getDeviceAddress () );
            iBatteryTempService.saveSingle ( batteryBoxVo.getBatteryTempVo () );
        }
        //BMS电压
        if ( batteryBoxVo.getBatteryVoltageVo () != null ) {
            batteryBoxVo.getBatteryVoltageVo ().setDeviceAddress ( batteryBoxVo.getDeviceAddress () );
            iBatteryVoltageService.saveSingle ( batteryBoxVo.getBatteryVoltageVo () );
        }
        return batteryBoxVo;
    }


    @Override
    public List<BatteryBoxVo> queryList ( BatteryBoxVo batteryBoxVo ) {
        if ( batteryBoxVo == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        List<BatteryBoxVo> batteryBoxVos = batteryBoxMapper.queryList ( batteryBoxVo );
        return batteryBoxVos;
    }

    @Override
    public Integer queryCount ( BatteryBoxVo batteryBoxVo ) {
        if ( batteryBoxVo == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        Integer count = batteryBoxMapper.queryCount ( batteryBoxVo );
        return count;
    }

    @Override
    public List<BatteryBoxVo> queryMaxDataForDay ( BatteryBoxVo batteryBoxVo ) {
        List<BatteryBoxVo> batteryBoxVos = batteryBoxMapper.queryMaxDataForDay ( batteryBoxVo );
        return batteryBoxVos;
    }

    @Override
    public List<BatteryBoxVo> queryStationForDay ( BatteryBoxVo batteryBoxVo ) {
        List<BatteryBoxVo> batteryBoxVos = batteryBoxMapper.queryStationForDay ( batteryBoxVo );
        return batteryBoxVos;
    }

}
